//
//  ChatViewController.m
//  Comic
//
//  Created by Andy.He on 16/3/10.
//  Copyright © 2016年 程锦. All rights reserved.
//

#import "HAChatViewController.h"
#import "RegistViewController.h"
#import "EaseUI.h"
#import "IConversationModel.h"
#import "ChatViewController.h"

@interface HAChatViewController () <UITableViewDataSource,UITableViewDelegate,UITabBarDelegate,IChatManagerDelegate,EaseConversationListViewControllerDataSource>
@property (strong, nonatomic) IBOutlet UITableView *chatTable;

@property (strong, nonatomic) NSMutableArray *buddylist;

@end

@implementation HAChatViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"会话列表";
    [self.chatTable registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
    self.buddylist = [NSMutableArray array];
    [[EaseMob sharedInstance].chatManager setIsAutoFetchBuddyList:YES];

    [self userStatus];

}


- (NSString *)conversationListViewController:(EaseConversationListViewController *)conversationListViewController
      latestMessageTitleForConversationModel:(id<IConversationModel>)conversationModel{
    return conversationModel.title;
}

- (id<IConversationModel>)conversationListViewController:(EaseConversationListViewController *)conversationListViewController
modelForConversation:(EMConversation *)conversation{
    
    return nil;
    
}

- (void)userStatus{
    if ([[EaseMob sharedInstance].chatManager isLoggedIn] == NO) {
        RegistViewController *registVC = [[RegistViewController alloc] initWithNibName:NSStringFromClass([RegistViewController class]) bundle:nil];
        [self.navigationController presentViewController:registVC animated:YES completion:nil];
    }else{
        [[EaseMob sharedInstance].chatManager asyncFetchBuddyListWithCompletion:^(NSArray *buddyList, EMError *error) {
            if (!error) {
                self.buddylist = [NSMutableArray arrayWithArray:buddyList];
                [self.chatTable reloadData];
            }
        } onQueue:nil];

    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.buddylist.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
    NSLog(@"%@",self.buddylist);
    EMBuddy *buddy = self.buddylist[indexPath.row];
    cell.textLabel.text = buddy.username;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    EMBuddy *buddy = self.buddylist[indexPath.row];
    ChatViewController *chatController = [[ChatViewController alloc] initWithConversationChatter:buddy.username conversationType:eConversationTypeChat];
    self.navigationController.navigationBar.translucent = YES;
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:chatController animated:YES];
    self.hidesBottomBarWhenPushed = NO;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
